package com.neutch.web;


import com.neutch.dao.UserDAO;
import com.neutch.dao.impl.UserDAOlmpl;
import com.neutch.entity.User;

import javax.print.DocFlavor;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "userServlet",urlPatterns = {"*.user"})
public class UserServlet extends HttpServlet {
    private UserDAO dao = new UserDAOlmpl();
    public UserServlet(){
        System.out.println("对象已经实例化了.....");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        //进行前端用户请求的拦截判断
        //根据用户请求url的不同 进行不同的罗技操作
        String path = request.getRequestURL().toString();
        path = path.substring(path.lastIndexOf("/"),path.lastIndexOf("."));
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //PrintWriter out = response.getWriter();
        if ("/findAll()".equals(path)){
            //查询操作
            List<User> users = dao.selectAll();

           //向前台输出数据
            /*//转发:业务逻辑处理未完成 交给更适合编写样式的jsp
            //1.绑定数据
            request.setAttribute("users",users);
            //2.获取转发器
            RequestDispatcher rd =  request.getRequestDispatcher("user_list.jsp");
            //3.转发
            rd.forward(request,response);*/
            request.setAttribute("users",dao.selectAll());
            request.getRequestDispatcher("list_user.jsp").forward(request,response);

        }else if ("/insert".equals(path)){

            //从前端获取表单提交的数据
            String name =  request.getParameter("name");
            String password =  request.getParameter("password");
            String phone =  request.getParameter("phone");
            String adress =  request.getParameter("adress");
            //添加操作
            dao.insert(new User(name,password,phone,adress));
            response.sendRedirect("findAll().user");
        }else if ("/delete".equals(path)){
            //删除操作
            int id = Integer.parseInt(request.getParameter("id"));
            dao.delete(id);
            response.sendRedirect("findAll().user");
        }else if ("/load".equals(path)){
            //更新
            int id = Integer.parseInt(request.getParameter("id"));

            User user = dao.selectById(id);
            request.setAttribute("user",user);
            request.getRequestDispatcher("update_user.jsp").forward(request,response);

        }else if ("/update".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            //user属性设置
            String name = request.getParameter("name");
            user.setName(name);
            String password = request.getParameter("password");
            user.setPassword(password);
            String phone = request.getParameter("phone");
            user.setPhone(phone);
            String adress = request.getParameter("adress");
            user.setAdress(adress);
            dao.update(user);

            response.sendRedirect("findAll().user");
        }else{
            System.out.println("路径错误....");
            response.getWriter().print("<h1>404</h1>");
            //重定向:业务逻辑处理完成之后,页面跳转的过程
            //只要没匹配到固定的url 都会定位到404页面
            response.sendRedirect("404.jsp");
        }
        /*else if (){
            //删除操作
 b
        }else if (){
            //根据id查找

        }else if (){
            //更新操作

        }*/
    }


}
